package com.sxt.demo.djiapplication.activity;

import static com.sxt.demo.djiapplication.DjiDemoApplication.TMP_FILE_DIR;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.sxt.demo.djiapplication.exceptionhandler.CrashHandler;
import com.sxt.demo.djiapplication.utils.FileUtil;

import java.io.File;
import java.util.List;


public abstract class BaseActivity extends AppCompatActivity {

    private long exitTime = 0;

    protected int statusBarHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        statusBarHeight = getStatusBarHeight();
        setScreenRotate(true);
        setStatusBar();
        init(savedInstanceState);
        addListener();
    }

    /**
     * 设置屏幕横竖屏切换
     *
     * @param screenRotate true  竖屏     false  横屏
     */
    @SuppressLint("SourceLockedOrientationActivity")
    @SuppressWarnings("SameParameterValue")
    protected void setScreenRotate(Boolean screenRotate) {
        if (screenRotate) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏模式
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

    /**
     * 沉浸式实现
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void setStatusBar() {
        Window window = getWindow();
        View decorView = window.getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 清除上一次app运行时的临时文件缓存
        clearCacheFiles();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            exitActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 退出app处理
     */
    protected void exitActivity() {
        if ((System.currentTimeMillis() - exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
        {
            Toast.makeText(this, "再点一次退出", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void clearCacheFiles() {
        FileUtil.deleteDirectoryFiles(new File(TMP_FILE_DIR));
        List<File> errorTrace = FileUtil.isFileExistsWithSuffix(
                getCacheDir().getAbsolutePath(),
                CrashHandler.FILE_NAME_SUFFIX
        ).getSecond();
        for (File file : errorTrace) {
            //noinspection ResultOfMethodCallIgnored
            file.delete();
        }
    }

    /**
     * 获取状态栏高度
     */
    private int getStatusBarHeight() {
        int statusBarHeight = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    protected void init(Bundle bundle) {
    }

    protected void addListener() {
    }

}
